package com.winnie.mapper;

import com.winnie.bean.UserEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.ResultSetType;

import java.util.List;
import java.util.Set;

@Mapper
public interface UserMapper {
    /**
     * 根据用户密码校验用户，并返回
     * @param username
     * @param password
     * @return
     */
    @Select("select * from t_sys_user where username = #{username} and password = #{password}")
    UserEntity findUser(String username, String password);

    /**
     * 通过用户名查找用户信息
     * @param username
     * @return
     */
    @Select("select * from t_sys_user where username = #{username}")
    UserEntity getUser(String username);

    /**
     * 查找角色
     * @param username
     * @return
     */
    @Select({"select t3.rolename from t_sys_user t1 INNER JOIN\n" +
            "t_sys_user_role t2 on t1.id = t2.userid LEFT JOIN \n" +
            "t_sys_role t3 on t2.roleid = t3.id \n" +
            "where t1.username = #{username}"})
    @ResultType(String.class)
    Set<String> findRole(String username);

    /**
     * 查找权限
     * @param username
     * @return
     */
    @Select("select t5.url from t_sys_user t1 INNER JOIN\n" +
            "t_sys_user_role t2 on t1.id = t2.userid LEFT JOIN \n" +
            "t_sys_role t3 on t2.roleid = t3.id LEFT JOIN\n" +
            "t_sys_role_permission t4 on t3.id = t4.roleid LEFT JOIN\n" +
            "t_sys_permission t5 on t4.permissionid = t5.id\n" +
            "where t1.username = #{username}")
    Set<String> findPermission(String username);

    /**
     * 添加用户
     * @param username
     * @param password
     */
    @Insert("insert into t_sys_user(username, password) values(#{username},#{password})")
    void insert(String username, String password);
}
